package com.njym.java.security.app.rest;

import com.njym.java.security.app.biz.NetworkUserBiz;
import com.njym.java.security.app.entity.NetworkUser;
import com.njym.java.security.app.entity.NetworkUserVo;
import com.njym.java.security.app.util.LunarUtil;
import com.njym.java.security.common.msg.ObjectRestResponse;
import com.njym.java.security.common.rest.BaseController;
import com.njym.java.security.common.util.ResponseUtil;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("networkUser")
public class NetworkUserController extends BaseController<NetworkUserBiz, NetworkUser> {
    /**
     * 查询到所有好友（deleted = 0）
     * @param fid
     * @param netId
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ResponseBody
    public Object lists(@RequestParam String fid, @RequestParam String netId) {
        if (!fid.equals("undefined") && !netId.equals("undefined")) {
            List<NetworkUser> list = baseBiz.selectListsByfid(Integer.parseInt(fid), Integer.parseInt(netId));
            Map<String, Object> data = new HashMap<>(1);
            data.put("list", list);
            return new ObjectRestResponse<NetworkUser>().rel(true).data(data);
        } else {
            return ResponseUtil.fail(40301, "错误！");
        }

    }

    /**
     * 根据id获取好友信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/getInfo",method = RequestMethod.GET)
    @ResponseBody
    public Object getInfo(@RequestParam String id) {
        if (!id.equals("undefined")) {
            NetworkUser record = baseBiz.selectById(Integer.parseInt(id));
            Map<String, Object> data = new HashMap<>(1);
            data.put("record", record);
            return new ObjectRestResponse<NetworkUser>().rel(true).data(data);
        } else {
            return ResponseUtil.fail(40301, "错误！");
        }

    }

    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    @ResponseBody
    public Object delete(@RequestParam String id) {
        if (!id.equals("undefined")) {
            NetworkUser entity = new NetworkUser();
            entity.setId(Integer.parseInt(id));
            entity.setDeleted(1);
            baseBiz.updateSelectiveById(entity);
            return new ObjectRestResponse<>().rel(true);
        } else {
            return ResponseUtil.fail(40301, "错误！");
        }

    }

    @RequestMapping(value = "/add",method = RequestMethod.GET)
    @ResponseBody
    public Object add(@RequestParam String fId, @RequestParam String netId,
                    @RequestParam String name, @RequestParam String photo,
                    @RequestParam String hoppy, @RequestParam String family,
                    @RequestParam String mobile, @RequestParam String phone,
                     @RequestParam Date birthday) {
        if (!fId.equals("undefined") && !netId.equals("undefined")) {
            NetworkUser entity = new NetworkUser();
            entity.setFid(Integer.parseInt(fId));
            entity.setNetId(Integer.parseInt(netId));
            entity.setPhoto(photo);
            entity.setFamily(family);
            entity.setCode(getUpEname(name));
            entity.setName(name);
            entity.setHoppy(hoppy);
            entity.setPhone(phone);
            entity.setMobile(mobile);
            entity.setAttr1(birthday);
            entity.setDeleted(0);
            baseBiz.insertSelective(entity);
            return new ObjectRestResponse<>().rel(true);
        } else {
            return ResponseUtil.fail(40301, "错误！");
        }

    }

    @RequestMapping(value = "/edit",method = RequestMethod.GET)
    @ResponseBody
    public Object edit(@RequestParam String id, @RequestParam String fId,
                    @RequestParam String name, @RequestParam String photo,
                    @RequestParam String hoppy, @RequestParam String family,
                    @RequestParam String mobile, @RequestParam String phone,
                      @RequestParam Date birthday) {
        if (!id.equals("undefined") && !fId.equals("undefined")) {
            NetworkUser entity = new NetworkUser();
            entity.setId(Integer.parseInt(id));
            entity.setFid(Integer.parseInt(fId));
            entity.setPhoto(photo);
            entity.setFamily(family);
            entity.setCode(getUpEname(name));
            entity.setName(name);
            entity.setHoppy(hoppy);
            entity.setPhone(phone);
            entity.setMobile(mobile);
            entity.setAttr1(birthday);
            entity.setDeleted(0);
            baseBiz.updateSelectiveById(entity);
            return new ObjectRestResponse<>().rel(true);
        } else {
            return ResponseUtil.fail(40301, "错误！");
        }

    }


    /**
     * 获得人脉的生日信息
     * @param fid
     * @return
     */
    @RequestMapping(value = "/getTips",method = RequestMethod.GET)
    @ResponseBody
    public Object getBirthdays(@RequestParam String fid) {
        if (!fid.equals("undefined")) {
            Calendar calendar = Calendar.getInstance();
            int now = calendar.get(Calendar.MONTH) + 1;
            List<NetworkUserVo> lists;
            if(now == 12){
                lists = baseBiz.getBirthdays_1(Integer.parseInt(fid));
            }else {
                lists = baseBiz.getBirthdays_2(Integer.parseInt(fid));
            }
            List<String> jieri = new ArrayList<>();
            String[] weekDays = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
            //我要获取当前的日期
            Date date = new Date();
            //设置要获取到什么样的时间
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            //获取String类型的时间
            //当前日期
            String createdate = sdf.format(date);
            //一个月后的日期
            String newDate = null;
            try {
                Calendar ct=Calendar.getInstance();
                ct.setTime(sdf.parse(createdate));
                ct.add(Calendar.MONTH, +1);
                newDate =  sdf.format(ct.getTime());
                Calendar today = Calendar.getInstance();
                today.setTime(sdf.parse(newDate));
                Calendar newtoday = Calendar.getInstance();
                newtoday.setTime(sdf.parse(createdate));
                //一月后的农历
                LunarUtil lunar = new LunarUtil(today);
                //当前农历
                LunarUtil newlunar = new LunarUtil(newtoday);
                System.out.println("北京时间：" + sdf.format(today.getTime())
                        + "　农历" + lunar);
                int year =  lunar.getYear();
                int day =  lunar.getDay();
                int month =   lunar.getMonth();
                //一个月后的农历
                String time  = year+"-"+month+"-"+day;
                int newyear =  newlunar.getYear();
                int newday =  newlunar.getDay();
                int newmonth =   newlunar.getMonth();
                //当前农历
                String newtime  = newyear+"-"+newmonth+"-"+newday;

                Calendar cal = Calendar.getInstance();
                int atyear =0;
                int atmonth = cal.get(Calendar.MONTH);
                if(atmonth < 11){
                    atyear = cal.get(Calendar.YEAR);
                }else{
                    atyear = cal.get(Calendar.YEAR)+1;
                }
                //根据日期判断日节日
                Date yuandan =  sdf.parse(atyear+"-01-01");
                Date laodong =  sdf.parse(atyear+"-05-01");
                Date guoqin =  sdf.parse(atyear+"-10-01");
                Date newnongli =  sdf.parse(newtime);
                Date nongli =  sdf.parse(time);

                Date chunjie =  sdf.parse(newyear+"-01-01");
                Date qingming =  sdf.parse(newyear+"-04-12");
                Date duanwu =  sdf.parse(newyear+"-05-05");
                Date zhongqiu =  sdf.parse(newyear+"-08-15");
                Date ss = ct.getTime();
                if(date.before(yuandan) && ct.getTime().after(yuandan)){
                    cal.setTime(yuandan);
                    int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
                    jieri.add("{ \"name\": \"元旦\", \"attr1\": \""+atyear+"-01-01"+"\", \"week\": \""+ weekDays[w] +"\"}");
                }
                if(date.before(laodong) && ct.getTime().after(laodong)){
                    cal.setTime(laodong);
                    int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
                    jieri.add("{ \"name\": \"劳动节\", \"attr1\": \""+atyear+"-05-01"+"\", \"week\": \""+ weekDays[w] +"\"}");
                }
                if(date.before(guoqin) && ct.getTime().after(guoqin)){
                    cal.setTime(guoqin);
                    int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
                    jieri.add("{ \"name\": \"国庆节\", \"attr1\": \""+atyear+"-10-01"+"\", \"week\": \""+ weekDays[w] +"\"}");
                }
                //阴历
                if(newnongli.before(chunjie) && nongli.after(chunjie)){
                    cal.setTime(guoqin);
                    int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
                    jieri.add("{ \"name\": \"春节\", \"attr1\": \""+newyear+"-01-01"+"\", \"week\": \""+ weekDays[w] +"\"}");
                }

                if(newnongli.before(qingming) && nongli.after(qingming)){
                    cal.setTime(guoqin);
                    int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
                    jieri.add("{ \"name\": \"清明\", \"attr1\": \""+newyear+"-04-12"+"\", \"week\": \""+ weekDays[w] +"\"}");
                }

                if(newnongli.before(duanwu) && nongli.after(duanwu)){
                    cal.setTime(guoqin);
                    int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
                    jieri.add("{ \"name\": \"端午节\", \"attr1\": \""+newyear+"-05-05"+"\"}, \"week\": \""+ weekDays[w] +"\"");
                }
                if(newnongli.before(zhongqiu) && nongli.after(zhongqiu)){
                    cal.setTime(guoqin);
                    int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
                    jieri.add("{ \"name\": \"中秋节\", \"attr1\": \""+newyear+"-08-15"+"\", \"week\": \""+ weekDays[w] +"\"}");
                }


            } catch (ParseException e) {
                e.printStackTrace();
            }


            Map<String, Object> data = new HashMap<>(1);
            data.put("list", lists);
            data.put("yinlilist", jieri);
            return new ObjectRestResponse<NetworkUser>().rel(true).data(data);
        } else {
            return ResponseUtil.fail(40301, "错误！");
        }


    }





    //获取汉语拼音的静态方法
    public static String getUpEname(String name) {
        char[] strs = name.toCharArray();
        String newname = null;
        if (strs.length == 2) {
            newname = toUpCase(getEname("" + strs[0])) + " " + toUpCase(getEname("" + strs[1]));
        } else if (strs.length == 3) {
            newname = toUpCase(getEname("" + strs[0])) + " " + toUpCase(getEname("" + strs[1] + strs[2]));
        }else if (strs.length == 4) {
            newname = toUpCase(getEname("" + strs[0] + strs[1])) + " " + toUpCase(getEname("" + strs[2] + strs[3]));
        } else {
            newname = toUpCase(getEname(name));
        }
        return newname;
    }

    //将中文转换为英文
    public static String getEname(String name) {
        HanyuPinyinOutputFormat pyFormat = new HanyuPinyinOutputFormat();
        pyFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        pyFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        pyFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
        try {
            return PinyinHelper.toHanyuPinyinString(name, pyFormat, "");
        } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
            badHanyuPinyinOutputFormatCombination.printStackTrace();
            return "";
        }
    }

    //首字母大写
     private static String toUpCase(String str) {
         StringBuffer newstr = new StringBuffer();
         newstr.append((str.substring(0, 1)).toUpperCase()).append(
                         str.substring(1, str.length()));
         return newstr.toString();
     }
}